package com.engine.jucailinkq.attendance.attendanceplan.cmd;

import com.engine.jucailinkq.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class GetWorkHoursItemByPersonCmd extends AbstractCommonCommand<Map<String,Object>> {
    public GetWorkHoursItemByPersonCmd(Map<String,Object> params){
        this.params=params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String resourceId = Util.null2String(params.get("resourceId"));
        String startDate = Util.null2String(params.get("startDate"));
        String endDate = Util.null2String(params.get("endDate"));
        String modeId = Utils.getFormmodeIdMap().get("uf_jcl_kq_zhgs");
        Map<String,Object> resultMap = Maps.newHashMap();

        String sql = "select dxlx,dataid,dx from uf_jcl_syzz where modeid=?";
        List<Map<String,Object>> organizationList = DbTools.getSqlToList(sql,modeId);
        sql = "select id,departmentid,subcompanyid1 from hrmresource where id =?";
        Map<String,Object> departMentMap = DbTools.getSqlToMap(sql,resourceId);
        Set<String> dataIds = Sets.newHashSet();
        Map<String, List<Map<String,Object>>> organizationListGroupBydxlx =  organizationList.stream().collect(Collectors.groupingBy(e ->e.get("dxlx").toString()));
        //对象类型为人员
        List<Map<String,Object>> personOrganizationList = organizationListGroupBydxlx.get("0");
        //对象类型为人员组织
        List<Map<String,Object>> personGroupOrganizationList = organizationListGroupBydxlx.get("1");
        //对象类型为部门
        List<Map<String,Object>> departmentOrganizationList = organizationListGroupBydxlx.get("2");
        //对象类型为分部
        List<Map<String,Object>> subCompanyOrganizationList = organizationListGroupBydxlx.get("3");
        if (personOrganizationList != null){
            for (Map<String,Object> personOrganization :personOrganizationList){
                String dx = Util.null2String(personOrganization.get("dx"));
                String ids = dx.split("-")[0];
                if (ids.equals(resourceId)){
                    dataIds.add(Util.null2String(personOrganization.get("dataid")));
                }
            }
        }
        try {
            if (departmentOrganizationList != null){
                String deptid = Util.null2String(departMentMap.get("departmentid"));
                String pdeptids = "";
                pdeptids = new DepartmentComInfo().getAllParentDepartId(Util.null2String(departMentMap.get("departmentid")), pdeptids);
                pdeptids = deptid + pdeptids;
                log.debug("pdeptids : [{}]",pdeptids);
                for (Map<String,Object> departmentOrganization :departmentOrganizationList){
                    String dx = Util.null2String(departmentOrganization.get("dx"));
                    String ids = dx.split("-")[0];
                    for (String pdeptid : pdeptids.split(",")){
                        if (pdeptid.equals(ids)){
                            dataIds.add(Util.null2String(departmentOrganization.get("dataid")));
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("catch error :{}",e);
        }
        if (subCompanyOrganizationList != null){
            String subCompanyId = Util.null2String(departMentMap.get("subcompanyid1"));
            for (Map<String,Object> subCompanyOrganization :subCompanyOrganizationList){
                String dx = Util.null2String(subCompanyOrganization.get("dx"));
                String ids = dx.split("-")[0];
                if (ids.equals(subCompanyId)){
                    dataIds.add(Util.null2String(subCompanyOrganization.get("dataid")));
                }
            }
        }
        if (personGroupOrganizationList != null){
            Set<String> personGroupIds = personGroupOrganizationList.stream().map(e -> Util.null2String(e.get("dx")).split("-")[0]).collect(Collectors.toSet());
            sql = "select mainid,empid,filters,bdate,edate,sqltj from uf_ryqz_dt1  where  mainid in ("+String.join(",",personGroupIds)+")";
            log.debug("query personGroupData : {}",sql);
            List<Map<String,Object>> personGroupData = DbTools.getSqlToList(sql);
            Set<String> personnelGroupIds = PersongroupCommonUtil.getPersonnelGroupingByPerson(personGroupData,resourceId,startDate);
            log.debug("personnelGroupIds : {}",personnelGroupIds);
            for (Map<String,Object> personGroupOrganization :personGroupOrganizationList){
                String personnelGroupId = Util.null2String(personGroupOrganization.get("dx")).split("-")[0];
                if (personnelGroupIds.contains(personnelGroupId)){
                    dataIds.add(Util.null2String(personGroupOrganization.get("dataid")));
                }
            }
        }

        List<Map<String,Object>> attendanceItems  = Lists.newArrayList();

        log.debug("dataIds : {}",dataIds);
        sql = "select *  from  uf_jcl_kq_zhgs  where id in ("+String.join(",",dataIds)+")";
        if (dataIds.size() > 0){
            log.debug("query GetWorkHoursItemByPersonCmd sql : {}",sql);
            attendanceItems.addAll(DbTools.getSqlToList(sql));
        }
        resultMap.put("data",attendanceItems);
        resultMap.put("total",attendanceItems.size());

        return resultMap;
    }
}
